package com.example.hxds.bff.driver.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import com.example.hxds.bff.driver.controller.form.*;
import com.example.hxds.bff.driver.service.DriverLocationService;
import com.example.hxds.bff.driver.service.DriverService;
import com.example.hxds.bff.driver.service.NewOrderMessageService;
import com.example.hxds.common.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * @Author XUYANG
 * @Date 2024-03-09 10:41
 */
@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController",description = "司机模块web接口")
public class DriverController {
    @Resource
    private DriverService driverService;
    @Resource
    private DriverLocationService locationService;
    @Resource
    private NewOrderMessageService newOrderMessageService;
    @PostMapping("/registerNewDriver")
    @Operation(summary = "新司机注册")
    public R registerNewDriver(@RequestBody @Valid RegisterNewDriverForm form){
        long driverId=driverService.registerNewDriver(form);
        //在SaToken上执行登录，缓存uerId拿到token
        StpUtil.login(driverId);
        String token=StpUtil.getTokenInfo().getTokenValue();
        return R.ok().put("token",token);
    }
    @PostMapping("/updateDriverAuth")
    @Operation(summary = "更新实名认证信息")
    @SaCheckLogin
    public R updateDriverAuth(@RequestBody @Valid UpdateDriverAuthForm form){
        long driverId=StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        int rows= driverService.updateDriverAuth(form);
        return R.ok().put("rows",rows);
    }
    @PostMapping("/createDriverFaceModel")
    @Operation(summary = "创建人脸模型归档")
    @SaCheckLogin
    public R createDriverFaceModel(@RequestBody @Valid CreateDriverFaceModelForm form){
        long driverId =StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        String res= driverService.createDriverFaceModel(form);
        return R.ok().put("result",res);
    }
    @PostMapping("/login")
    @Operation(summary = "登录系统")
    public R login(@RequestBody @Valid LoginForm form){
        HashMap map=driverService.login(form);
        if (map!=null){
            long driverId= MapUtil.getLong(map,"id");
            byte realAuth=Byte.parseByte(MapUtil.getStr(map,"realAuth"));
            boolean archive=MapUtil.getBool(map,"archive");
            StpUtil.login(driverId);
            String token=StpUtil.getTokenInfo().getTokenValue();
            return R.ok().put("token",token).put("realAuth",realAuth).put("archive",archive);
        }
        return R.ok();
    }
    @PostMapping("/searchDriverBaseInfo")
    @SaCheckLogin
    @Operation(summary = "查询司机基本信息")
    public R searchDriverBaseInfo(){
        long driverId=StpUtil.getLoginIdAsLong();
        SearchDriverBaseInfoForm form=new SearchDriverBaseInfoForm();
        form.setDriverId(driverId);
        HashMap map=driverService.searchDriverBaseInfo(form);
        return R.ok().put("result",map);
    }
    @PostMapping("/searchWorkBenchData")
    @Operation(summary = "查询司机工作台数据")
    @SaCheckLogin
    public R searchWorkBenchData(){
        long driverId=StpUtil.getLoginIdAsLong();
        HashMap res=driverService.searchWorkBenchData(driverId);
        return R.ok().put("result",res);
    }
    @GetMapping("/searchDriverAuth")
    @Operation(summary = "查询司机认证信息")
    @SaCheckLogin
    public R searchDriverAuth(){
        long driverId=StpUtil.getLoginIdAsLong();
        SearchDriverAuthForm form=new SearchDriverAuthForm();
        form.setDriverId(driverId);
        HashMap res=driverService.searchDriverAuth(form);
        return R.ok().put("result",res);
    }
    @PostMapping("/startWork")
    @Operation(summary = "开始接单")
    @SaCheckLogin
    public R startWork(){
        long driverId=StpUtil.getLoginIdAsLong();
        System.out.println(driverId+"开始接单了");
        //删除司机定位缓存
        RemoveLocationCacheForm form=new RemoveLocationCacheForm();
        form.setDriverId(driverId);
        locationService.removeLocationCache(form);

        //清空新订单消息列表
        ClearNewOrderQueueForm form_2=new ClearNewOrderQueueForm();
        form_2.setUserId(driverId);
        newOrderMessageService.clearNewOrderQueue(form_2);
        return R.ok();
    }
    @PostMapping("/stopWork")
    @Operation(summary = "开始接单")
    @SaCheckLogin
    public R stopWork(){
        long driverId=StpUtil.getLoginIdAsLong();
        System.out.println(driverId+"停止接单了");
        //删除司机定位缓存
        RemoveLocationCacheForm form_1=new RemoveLocationCacheForm();
        form_1.setDriverId(driverId);
        locationService.removeLocationCache(form_1);

        //清空新订单消息列表
        ClearNewOrderQueueForm form_2=new ClearNewOrderQueueForm();
        form_2.setUserId(driverId);
        newOrderMessageService.clearNewOrderQueue(form_2);
        return R.ok();
    }
}
